package jmu.cultural_tourism.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jmu.cultural_tourism.entity.PostInfo;
import jmu.cultural_tourism.service.PostImageService;
import jmu.cultural_tourism.service.PostInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;


@RestController
@Tag(name="帖子信息接口")
@RequestMapping("postInfo")
public class PostInfoController {
    /*服务对象*/
    @Autowired
    private PostInfoService postInfoService;
    @Autowired
    private PostImageService postImageService;

    //新增数据
    @Operation(summary = "新增数据")
    @PostMapping("add")
    public ResponseEntity<PostInfo> add(@RequestBody PostInfo postInfo) {
        // 调用Service处理多表插入（包含事务管理）
        postInfoService.insert(postInfo);
        return ResponseEntity.ok(postInfo);
    }
    //删除帖子
    @Operation(summary = "删除帖子")
    @DeleteMapping("del")
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.postInfoService.deleteById(id));
    }

    //编辑帖子信息
    @Operation(summary = "编辑帖子信息")
    @PutMapping("edit")
    public ResponseEntity<PostInfo> edit(@RequestBody PostInfo postInfo) {
        return ResponseEntity.ok(this.postInfoService.update(postInfo));
    }

    @Operation(summary = "分页展示所有帖子")
    @PostMapping({"post/{pageNo}/{pageSize}","post/{pageNo}","post"})
    public ResponseEntity<List<PostInfo>> queryAllByPage(
            @PathVariable(value = "pageNo",required = false) Integer pageNo,
            @PathVariable(value = "pageSize",required = false) Integer pageSize)
    {
        if (pageNo == null) {
            pageNo = 0; // 第一页
        } else {
            pageNo = pageNo - 1;
        }
        if (pageSize == null) {
            pageSize = 5;
        }
        //构造一个PageRequest 对象
        PageRequest pageRequest = PageRequest.of(pageNo, pageSize);
        return ResponseEntity.ok(this.postInfoService.queryAll(pageRequest));
    }

    @Operation(summary = "给帖子点藏")
    @PutMapping("postLike")
    public ResponseEntity<PostInfo> postLike(@RequestBody PostInfo postInfo) {
        postInfo.setPostLike(postInfo.getPostLike() + 1);
        return ResponseEntity.ok(this.postInfoService.update(postInfo));
    }

    @Operation(summary = "给帖子收藏")
    @PutMapping("postCollect")
    public ResponseEntity<PostInfo> post(@RequestBody PostInfo postInfo) {
        postInfo.setCollect(postInfo.getCollect() + 1);
        return ResponseEntity.ok(this.postInfoService.update(postInfo));
    }

    @Operation(summary = "取消收藏")
    @PutMapping("postCollectCancel")
    public ResponseEntity<PostInfo> postCancel(@RequestBody PostInfo postInfo) {
        postInfo.setCollect(postInfo.getCollect() - 1);
        return ResponseEntity.ok(this.postInfoService.update(postInfo));
    }

    @Operation(summary = "取消帖子点藏")
    @PutMapping("postLikeCancel")
    public ResponseEntity<PostInfo> postLikeCancel(@RequestBody PostInfo postInfo) {
        postInfo.setPostLike(postInfo.getPostLike() - 1);
        return ResponseEntity.ok(this.postInfoService.update(postInfo));
    }



    //根据帖子ID查找帖子
    @Operation(summary = "根据帖子ID查找帖子")
    @GetMapping("findById/{id}")
    public ResponseEntity<PostInfo> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.postInfoService.queryById(id));
    }

    //分页查询
    @PostMapping({"page/{pageNo}/{pageSize}","page/{pageNo}","page"})
    public ResponseEntity<Page<PostInfo>> queryByPage(
            @RequestBody PostInfo postInfo,
            @PathVariable(value = "pageNo",required = false) Integer pageNo,
            @PathVariable(value = "pageSize",required = false) Integer pageSize)
    {
        if (pageNo == null) {
            pageNo = 0; // 第一页
        } else {
            pageNo = pageNo - 1;
        }
        if (pageSize == null) {
            pageSize = 5;
        }
        //构造一个PageRequest 对象
        PageRequest pageRequest = PageRequest.of(pageNo, pageSize);
        return ResponseEntity.ok(this.postInfoService.queryByPage(postInfo, pageRequest));
    }

    //模糊查询帖子标题和内容
    @PostMapping({"fuzzy/{pageNo}/{pageSize}","fuzzy/{pageNo}","fuzzy"})
    public ResponseEntity<Page<PostInfo>> fuzzySearch(
        @RequestParam String fuzzyContent,
        @PathVariable(value = "pageNo",required = false) Integer pageNo,
        @PathVariable(value = "pageSize",required = false) Integer pageSize)
    {
        if (pageNo == null) {
            pageNo = 0;
        } else {
            pageNo = pageNo - 1;
        }
        if (pageSize == null) {
            pageSize = 5;
        }
        PageRequest pageRequest = PageRequest.of(pageNo, pageSize);
        Page<PostInfo> postInfos = postInfoService.fuzzyPost(fuzzyContent, pageRequest);
        return ResponseEntity.ok(postInfos);
    }

    //按照热度降序排序
    @Operation(summary = "按照热度降序排序")
    @GetMapping("like/{pageNo}/{pageSize}")
    public ResponseEntity<Page<PostInfo>> queryAllByLikeDesc(
            @PathVariable(value = "pageNo",required = false) Integer pageNo,
            @PathVariable(value = "pageSize",required = false) Integer pageSize)
    {
        if (pageNo == null) {
            pageNo = 0;
        } else {
            pageNo = pageNo - 1;
        }
        if (pageSize == null) {
            pageSize = 5;
        }
        PageRequest pageRequest = PageRequest.of(pageNo, pageSize);
        Page<PostInfo> postInfos = postInfoService.queryAllByLikeDesc(pageRequest);
        return ResponseEntity.ok(postInfos);
    }

    //根据用户ID查找收藏的帖子
    @PostMapping({"record/{pageNo}/{pageSize}","record/{pageNo}","record"})
    public ResponseEntity<Page<PostInfo>> queryCollectedPostsByUserId(
            Integer userId,
            @PathVariable(value = "pageNo",required = false) Integer pageNo,
            @PathVariable(value = "pageSize",required = false) Integer pageSize)
    {
        if (pageNo == null) {
            pageNo = 0; // 第一页
        } else {
            pageNo = pageNo - 1;
        }
        if (pageSize == null) {
            pageSize = 5;
        }
        //构造一个PageRequest 对象
        PageRequest pageRequest = PageRequest.of(pageNo, pageSize);
        return ResponseEntity.ok(this.postInfoService.queryCollectedPostsByUserId(userId, pageRequest));
    }

    /**
     * 检查用户是否收藏了特定帖子
     * @param userId 用户ID
     * @param postId 帖子ID
     * @return 布尔值，表示是否已收藏
     */
    @GetMapping("/isCollected/{postId}/{userId}")
    public boolean checkPostCollected(
            @PathVariable Integer userId,
            @PathVariable Integer postId) {

        return postInfoService.isPostCollected(userId, postId);
    }

    /**
     * 添加帖子收藏
     * @param userId 用户ID
     * @param postId 帖子ID
     * @return 布尔值，表示是否添加成功
     */
    @PostMapping("/addCollection")
    public boolean addPostCollection(
            @RequestParam Integer userId,
            @RequestParam Integer postId) {
        return postInfoService.addPostCollection(userId, postId);
    }

    @DeleteMapping("/delCollection")
    public boolean deletePostCollection(
            @RequestParam Integer userId,
            @RequestParam Integer postId) {
        return postInfoService.deletePostCollection(userId, postId);
    }
}

